import { persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage'

import { LOGGED_IN_USER_PROFILE_DATA, BEFORE_LOGIN_PATH } from '../actions/AuthActions';

const initialState = {
    error: null,
    profileData: {},
    preAuthPath: '/'
}

const persistConfig = {
    key: 'profile',
    storage,
    whitelist: ['profileData', 'preAuthPath'], // place to select which state you want to persist
}

const authReducer = (state = initialState, action) => {
    switch (action.type) {
        case LOGGED_IN_USER_PROFILE_DATA:
            return {
                ...state,
                profileData: action.data,
            }
        case BEFORE_LOGIN_PATH:
            return {
                ...state,
                preAuthPath: action.data,
            }
        default:
            return {...state};
    }
};

export default persistReducer(persistConfig, authReducer);